<template>
    <div class="r_main">
        <div class="tabIn">
            <div class="tab">
                <div v-for="item in tabList" :key="item.type" @click="getTabtype(item.type)">
                    <span :class="{ actives: item.type == curType }">{{ item.name }} {{ item.nums }}</span>
                    <div class="botLine">
                        <span :class="{ active: item.type == curType }"></span>
                    </div>
                </div>
            </div>
            <div class="search">
                <input type="text" placeholder="搜索专栏..." placeholder-class="pla" v-model="keyword"
                    @keyup.enter="goSearch">
                <img src="@/assets/img/search.svg" alt="" @click="goSearch()">
            </div>
        </div>
        <div class="form_con">
            <div class="outer">

                <div class="list" v-for="(item, index) in list" :key="index">
                    <div class="img" @click="goPage('/columnIndex', item.id)">
                        <img :src="item.thumb" alt="">
                    </div>
                    <p class="line2" @click="goPage('/columnIndex', item.id)">【{{ item.title }}】{{ item.intro }}</p>
                    <div class="bom2 bom" v-if="curType == 1">
                        <div class="tip" v-for="(t, i) in item.topic" :key="i">
                            <span>{{ t.name }}</span>
                        </div>
                        <div class="tip2" @click="openTopic(item.id)">
                            <i class="el-icon-plus"></i>
                        </div>
                    </div>
                    <div class="bom" v-if="curType == 1">

                        <div v-if="!$store.getters.isMobile" class="y2" @click="goPage('/column/basic', item.id)">
                            <span>编辑</span>
                        </div>
                        <div v-if="item.is_top == 0" @click="changeTop(item.id, 1)" class="y2" slot="reference">
                            <span>置顶</span>
                        </div>
                        <div v-if="item.is_top == 1" icon="el-icon-info" icon-color="red" @click="changeTop(item.id, 0)"
                            class="y2" slot="reference">
                            <span>取消置顶</span>
                        </div>

                        <div v-if="item.status == 2" icon="el-icon-info" icon-color="red"
                            @click="changeStatus(item.id, 1)" class="y2" slot="reference">
                            <span>上架</span>
                        </div>

                        <div v-if="item.status == 1" icon="el-icon-info" icon-color="red"
                            @click="changeStatus(item.id, 2)" class="y2" slot="reference">
                            <span>下架</span>
                        </div>


                        <div @click="delColumn(item.id)" class="y2" slot="reference">
                            <span>删除</span>
                        </div>



                        <div v-if="item.is_hide == 0" @click="changeHide(item.id, 1)" class="y2" slot="reference">
                            <span>私密</span>
                        </div>

                        <div v-if="item.is_hide == 1" @click="changeHide(item.id, 0)" class="y2" slot="reference">
                            <span>公开</span>
                        </div>

                    </div>
                </div>

            </div>
            <el-empty description="没有内容了" v-if="list.length == 0"></el-empty>

        </div>
        <el-dialog :visible.sync="dialogVisible" :width="$store.getters.isMobile?'90%':'700px'" :close-on-click-modal="false">
            <span slot="title" class="dialog-header">
                <i style="color:#d41f19;margin-right: 12px;" class="el-icon-price-tag"></i>添加标签
            </span>
            <el-form>
                <el-form-item>
                    <div class="select">
                        <el-select :popper-append-to-body="false" filterable remote v-model="choose" multiple
                            allow-create default-first-option placeholder="请输入标签名称" @change="onChange"
                            :remote-method="getInput" width="100px" @visible-change="isopen">
                            <el-option v-for="item in topic" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                            <el-option disabled class="load-more" v-if="showMore">
                                <span @click="load2">加载更多...</span>
                            </el-option>
                        </el-select>

                    </div>

                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="onsubmit">确 定</el-button>
            </span>
        </el-dialog>
    </div>


</template>
<script>
import { MenuItem } from 'element-ui';


export default {
    data() {
        return {
            topic: [],
            choose: [],
            list: [],
            form: { type: '' },
            tabList: [],
            curType: 1,
            dialogVisible: false,
            addTip: '',
            loading: false,
            totalPage: 1,
            pageNum: 1,
            keyword: '',
            col_id: '',
            // 标签分页
            totalPaget: 1,
            pageNumt: 1,
            loadingt: false,
            t_keyword: '',
            newlist: [],
            showMore: false
        }
    },
    mounted() {
        window.addEventListener('scroll', this.handleScroll); // 监听滚动事件
        this.getPost()
        this.getTabs()

    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll); // 移除监听
    },

    methods: {
        reset() {

            this.totalPaget = 1,
                this.pageNumt = 1,
                this.loadingt = false,
                this.t_keyword = '',
                this.showMore = false
            this.topic = []
        },

        // 关闭列表的时候
        isopen(e) {
            if (!e) {
                this.reset()
            }
        },

        load2() {

            var page = this.pageNumt
            //如果请求未结束就返回不连续请求
            if (this.loadingt) {
                return
            }
            //数据请求完成无更多数据时
            if (page >= this.totalPaget) {
                return false;
            }
            page = parseInt(page) + 1
            this.pageNumt = page
            this.getTopic()
        },

        getTopic() {
            this.loadingt = true
            let params = {
                keyword: this.t_keyword,
                page: this.pageNumt,
            }

            this.$api.searchTopic(params).then(res => {
                let response = res.data.data
                if (res.data.code == 1) {
                    this.totalPaget = response.last_page
                    if (this.totalPaget <= this.pageNumt) {
                        this.showMore = false
                    } else {
                        this.showMore = true
                    }

                    this.loadingt = false
                    this.topic = this.topic.concat(response.data);

                }

            })
        },


        getInput(keyword) {
            console.log('用户正在输入:', keyword)
            this.topic = []

            // 没有关键词或清空关键词
            if (!keyword || keyword == '') {
                this.reset()
                return
            }

            this.t_keyword = keyword
            this.pageNum = 1
            this.getTopic()

        },


        // select选中后
        onChange(e) {
            // 重做选中的标签数组

            this.newlist = e.map(item => {
                return typeof item === 'string' ? { name: item } : { id: item };
            });
        },

        goSearch() {
            this.list = []
            this.pageNum = 1
            if (this.curType == 1) {
                this.getPost()
            } else {
                this.getSave()
            }

        },


        goPage(url, id) {
            // 另页打开
            const route = this.$router.resolve({
                path: url,
                query: { id }
            });

            window.open(route.href, '_blank');
        },
        openTopic(id) {
            this.col_id = id
            this.dialogVisible = true
        },

        getTabs() {

            this.$api.getColTabs().then(res => {
                if (res.data.code == 1) {
                    this.tabList = res.data.data
                }

            })
        },

        // 自有
        getPost() {
            this.loading = true
            let params = {
                keyword: this.keyword,
                page: this.pageNum
            }
            this.$api.MyColumn(params).then(res => {
                if (res.data.code == 1) {
                    this.totalPage = res.data.data.last_page
                    this.list = this.list.concat(res.data.data.data);
                    this.loading = false
                }

            })
        },

        // 收藏
        getSave() {
            this.loading = true
            let params = {
                keyword: this.keyword,
                page: this.pageNum
            }
            this.$api.SaveColumn(params).then(res => {
                if (res.data.code == 1) {
                    this.totalPage = res.data.data.last_page
                    this.list = this.list.concat(res.data.data.data);
                    this.loading = false
                }

            })
        },

        changeStatus(id, status) {
            let title = status == 1 ? '上架' : '下架'
            this.$confirm(`确认${title}该内容吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                id,
                status,
            }
            this.$api.changeCols(params).then(res => {
                if (res.data.code == 1) {
                    this.$message.closeAll()

                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    })
                    this.list = []
                    this.pageNum = 1
                    this.getPost()
                }
            })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
          
        },


        // 置顶专栏操作
        changeTop(id, status) {
            let title = status == 1 ? '置顶' : '取消置顶'
            this.$confirm(`确认${title}该内容吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    id,
                    status
                }
                this.$api.topCols(params).then(res => {
                    if (res.data.code == 1) {
                        this.$message.closeAll()

                        this.$message({
                            message: res.data.msg,
                            type: 'success'
                        })
                        this.list = []
                        this.pageNum = 1
                        this.getPost()
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });

        },

        // 私密专栏操作
        changeHide(id, status) {
            let title = status == 1 ? '私密' : '公开'
            this.$confirm(`确认${title}该内容吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                id,
                status
            }
            this.$api.hideCols(params).then(res => {
                if (res.data.code == 1) {
                    this.$message.closeAll()

                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    })
                    this.list = []
                    this.pageNum = 1
                    this.getPost()
                }
            })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
           
        },

        delColumn(id) {
            this.$confirm(`确认删除该内容吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                id
            }
            this.$api.delCols(params).then(res => {
                if (res.data.code == 1) {
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    })
                    this.list = []
                    this.pageNum = 1
                    this.getPost()
                }
            })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
           
        },

        getTabtype(t) {
            this.curType = t
            this.list = []
            this.pageNum = 1
            if (t == 1) {
                this.getPost()
            } else {
                this.getSave()
            }
        },
        load() {
            var page = this.pageNum
            //如果请求未结束就返回不连续请求
            if (this.loading) {
                return
            }
            //数据请求完成无更多数据时
            if (page >= this.totalPage) {
                return false;
            }
            page = parseInt(page) + 1
            this.pageNum = page
            if (this.curType == 1) {
                this.getPost()
            } else {
                this.getSave()
            }
            console.log("处理触底");
        },

        onsubmit() {
            let params = {
                id: this.col_id,
                topic: this.newlist
            }
            this.$api.addColtopic(params).then(res => {
                if (res.data.code == 1) {
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.addTip = ''
                    this.dialogVisible = false
                    this.list = []
                    this.pageNum = 1
                    this.getPost()
                }

            })
        },
        handleScroll() {
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            const windowHeight = window.innerHeight;
            const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            // 判断是否滚动到底部
            if (scrollTop + windowHeight >= scrollHeight - 20) {
                this.load()
            }
        },
    }
}
</script>
<style lang="scss" scoped>
.r_main {
    width: 100%;

    .user {
        width: 100%;
        height: 200px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: relative;
        padding: 0 30px;
        overflow: hidden;
        background: linear-gradient(to right, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));

        .bgg {
            position: absolute;
            width: 100%;
            height: auto;
            z-index: -1;

        }

        .left {
            display: flex;
            color: #fff;
            align-items: center;

            .avatar {
                width: 150px;
                height: 150px;
                margin-right: 27px;
                flex-shrink: 0;
                border-radius: 50%;
                overflow: hidden;

                img {
                    object-fit: cover;
                    width: 100%;
                    height: 100%;
                }
            }

            .info {
                .nane {
                    font-weight: bold;
                    font-size: 30px;
                    color: #FFFFFF;
                    margin-bottom: 13px;
                }

                .data {
                    display: flex;
                    align-items: center;
                    margin-bottom: 13px;

                    .line {
                        height: 18px;
                        display: block;
                        width: 2px;
                        background-color: #fff;
                        margin: 0 18px;
                    }

                    >div {
                        font-weight: 400;
                        font-size: 18px;
                        color: #999999;

                        >span {
                            color: #FFFFFF;
                        }
                    }
                }

                .address {
                    display: flex;
                    align-items: center;
                    margin-bottom: 10px;
                    font-weight: 400;
                    font-size: 16px;
                    color: #999999;

                    .line {
                        margin: 0 18px;
                        display: block;
                        height: 16px;
                        width: 2px;
                        background-color: #999;
                    }
                }

                .sign {
                    font-weight: 400;
                    font-size: 16px;
                    color: #FFFFFF;
                }
            }
        }

        .right {
            height: 100%;
            padding: 28px 0 38px;
            font-weight: 400;
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            flex-direction: column;
            font-size: 20px;
            color: #FFFFFF;

            .share {
                cursor: pointer;
                display: flex;
                align-items: center;
                justify-content: center;

                img {
                    width: 28px;
                    height: auto;
                    margin-left: 11px;
                }
            }

            .btn {
                display: flex;
                align-items: center;
                justify-content: center;

                button {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 120px;
                    height: 48px;
                    background: #F98951;
                    border-radius: 24px;
                    opacity: 0.9;
                    margin-left: 26px;
                    font-weight: 400;
                    font-size: 20px;
                    color: #FFFFFF;
                    border: none;

                    &:nth-child(2) {
                        background: #1068E7;
                    }
                }
            }
        }
    }

    .tabIn {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 23px 0;
        background-color: #fff;
        position: sticky;
        top: 98px;
        z-index: 99;
        border-top: 0.05rem solid #E8E8E8;

        .tab {
            display: flex;
            align-items: center;

            >div {
                display: flex;
                flex-direction: column;
                align-items: center;
                margin-right: 55px;
                cursor: pointer;
                position: relative;
                font-size: 24px;

                .actives {
                    font-weight: bold;
                    color: #1068E7;
                }

                .botLine {
                    width: 100%;
                    height: 3px;
                    box-sizing: content-box;
                    position: relative;
                    top: calc(100% + 13px);
                    position: absolute;
                    left: 0;
                    right: 0;

                    >span {
                        position: absolute;
                        left: 0;
                        top: 0;
                        display: inline-block;
                        width: 0;
                        height: 3px;
                        background-color: #1068E7;
                        border-radius: 3px;
                    }

                    .active {
                        animation: expandWidth .5s forwards;
                    }

                    @keyframes expandWidth {
                        0% {
                            width: 0;
                        }

                        100% {
                            width: 100%;
                        }
                    }
                }
            }
        }

        .search {
            margin-left: 4.4rem;
            position: relative;
            width: 280px;
            height: 48px;
            display: flex;
            align-items: center;

            img {
                width: 23px;
                height: 23px;
                position: absolute;
                top: calc(50% - 11px);
                right: 11px;
            }

            input {
                width: 280px;
                height: 48px;
                outline: none;
                background: #F6F6F6;
                border-radius: 1.25rem;
                padding: 0 3rem 0 1.2rem;
                box-sizing: border-box;
                font-size: 0.83rem;
                border: none;
                border: 1px solid #F6F6F6;

                &:focus {
                    border: 1px solid #1068E7;
                }
            }

            .pla {
                font-weight: 400;
                font-size: 0.83rem;
                color: #666666;
            }
        }
    }

    .form_con {
        min-height: 500px;
        // overflow-y: auto;
    }
}

.outer {
    display: flex;
    flex-wrap: wrap;
    padding-bottom: 20px;
}

.dialog-header {
    font-weight: bold;
    font-size: 22px;
}

.dialog-footer {
    font-weight: bold;
    font-size: 22px;
    display: flex;
    justify-content: center;
    align-items: center;

    button {
        margin: 0 50px;
        width: 140px;
        height: 50px;
        font-size: 16px;
        line-height: 50px;
        padding: 0;
    }
}

.list {
    cursor: pointer;
    width: 330px;
    margin-right: 25px;
    margin-bottom: 80px;
    border-radius: 10px 10px 0 0;
    overflow: hidden;

    &:nth-child(4n) {
        margin-right: 0;
    }

    .img {
        width: 330px;
        height: 331px;
        justify-content: center;
        align-items: center;
        display: flex;
        background-color: #E5E5E5;
        flex-direction: column;
        border-radius: 10px;
        overflow: hidden;

        img {
            width: 330px;
            height: 330px;
            object-fit: cover;
        }

        i {
            font-size: 100px;
            color: #999;
            border: 1px dashed #999;
            margin-bottom: 39px;
        }

        span {
            font-weight: 400;
            font-size: 24px;
            color: #666666;
        }
    }

    >p {
        font-weight: 400;
        font-size: 18px;
        color: #000000;
        margin: 24px 0 20px;
        height: 50px;
    }

    .bom2 {
        margin-bottom: 20px;
        justify-content: flex-start !important;
        max-width: 100%;
        flex-wrap: wrap;
        display: flex;
        min-height: 36px;

        .tip {
            margin-right: 10px;
            padding: 5px;
            font-size: 12px;
            background-color: #C3DCFB;
            margin: 5px 10px 5px 0;
        }

        .tip2 {
            i {
                border: 1px solid #ccc;
                font-size: 24px;
                color: #8f8d8d;
            }
        }
    }

    .bom {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .y2 {
            background-color: #e4e1e1;
            padding: 5px 15px;
            display: flex;
            align-items: center;

            >div {
                display: flex;
                align-items: center;
            }

            i {
                font-size: 16px;
                margin-right: 5px;
            }

            span {
                font-size: 14px;
                color: #807e7e;
            }
        }

        .collect {
            font-weight: 400;
            font-size: 16px;
            color: #8b8a8a;
        }
    }
}

.dialog-header {
    font-weight: bold;
    font-size: 22px;
}

.dialog-footer {
    font-weight: bold;
    font-size: 22px;
    display: flex;
    justify-content: center;
    align-items: center;

    button {
        margin: 0 50px;
        width: 140px;
        height: 50px;
        font-size: 16px;
        line-height: 50px;
        padding: 0;
    }
}

@media screen and (max-width:768px) {
    .r_main .tabIn {
        padding: 10px 10px 20px;

        .tab {
            >div {
                font-size: 15px;
            }

        }

        .search {
            height: 30px;
            width: 200px;

            input {
                width: 200px;
                font-size: 14px;
                line-height: 30px;
                height: 30px;
                padding: 0 20px 0 10px;
            }

            img {
                height: 15px;
                top: calc(50% - 7px);
                width: 15px;
            }
        }
    }

    .outer {
        .list {
            border: 1px solid #bfc3f3;
            border-radius: 10px;
            padding: 10px;
            width: 100%;
            height: 140px;
            display: flex;
            position: relative;
            margin: 0 10px 15px;

            .img {
                flex-shrink: 0;
                width: 120px;
                height: 120px;

                img {
                    width: 100%;
                    height: 100%;
                }
            }

            >p {
                font-size: 14px;
                margin-left: 10px;
                max-height: 40px;
                line-height: 20px;
                -webkit-line-clamp: 2;
                line-clamp: 2;
                height: auto !important;
            }

            .bom {
                position: absolute;
                bottom: 10px;
                left: 140px;
                width: calc(100% - 140px);
                justify-content: flex-start;

                .y2 {
                    margin-right: 10px;
                    padding: 5px 10px;

                    span {
                        font-size: 12px;

                    }

                    i {
                        font-size: 12px;
                        margin-right: 5px;
                    }
                }
            }
            .bom2{
    top: 60px;
    bottom: auto;
  .tip{
    font-size: 11px;
    padding: 2px 5px;
  } 
  .tip2{
    i{
        font-size: 14px;
    }
  } 
}
        }
    }
}
</style>